package com.example.project.controller;

import com.example.project.action.ActionUtil;
import com.example.project.base.ResultBean;
import com.example.project.common.MatchesKey;
import com.example.project.entity.FirmVO;
import com.example.project.entity.UserVO;
import com.example.project.framework.BaseController;
import com.example.project.service.UserService;
import com.example.project.util.FunctionUtil;
import com.example.project.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Api(value = "UserController",tags = "用户管理")
public class UserController extends BaseController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "/userSort",method = RequestMethod.POST)
    @ApiOperation("用户排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pkid",value="用户ID"),
            @ApiImplicitParam(name="sort",value="0向下/1向上"),
    })
    public ResultBean userSort(Integer pkid, Integer sort){
        userService.userSort(pkid,sort);
        return ok("成功");
    }


    @RequestMapping(value = "/editUser",method = RequestMethod.POST)
    @ApiOperation("修改个人用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pkid",value="用户ID",required=true),
    })
    public ResultBean editUser(){
        userService.editUser(ActionUtil.getParameterMap());
        return ok("成功");
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    @ApiOperation("添加个人用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value="用户名称",required=true),
            @ApiImplicitParam(name="nickName",value="用户昵称",required=true),
            @ApiImplicitParam(name="phone",value="联系电话",required=true),
            @ApiImplicitParam(name="area",value="所在区域（显示区/县级）‘吴兴区、南浔区、德清县、长兴县、安吉县'",required=true),
            @ApiImplicitParam(name="address",value="详细地址（完整地址）‘浙江省湖州市xx区/县’",required=true),
            @ApiImplicitParam(name="headUrl",value="用户头像",required=true),
            @ApiImplicitParam(name="email",value="电子邮箱",required=true),
            @ApiImplicitParam(name="inviteCode",value="中间人邀请码‘12345678’"),
    })
    public ResultBean addFirm(UserVO vo){
        FunctionUtil.check(StringUtil.isEmpty(vo.getUserName()),"请输入用户名称！");
        FunctionUtil.check(StringUtil.isEmpty(vo.getNickName()),"请输入用户昵称！");
        FunctionUtil.check(StringUtil.isEmpty(vo.getPhone()),"请输入联系电话！");
        FunctionUtil.check(StringUtil.isEmpty(vo.getArea()),"请输入所在区域！");
        FunctionUtil.check(StringUtil.isEmpty(vo.getAddress()),"请输入详细地址！");
        FunctionUtil.check(StringUtil.isEmpty(vo.getHeadUrl()),"请上传用户头像！");
        FunctionUtil.check(StringUtil.isEmpty(vo.getEmail()),"请输入电子邮箱！");
        FunctionUtil.check(!vo.getPhone().matches(MatchesKey.PHONE_MATCHES),"联系电话不正确！如：18200001111！");
        return ok("成功",userService.addUser(vo));
    }

    @RequestMapping(value = "/delUser",method = RequestMethod.POST)
    @ApiOperation("删除用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pkid",value="用户ID"),
    })
    public ResultBean delUser(Integer pkid){
        userService.deleteById(pkid);
        return ok("成功");
    }


    @RequestMapping(value = "/getUserList",method = RequestMethod.POST)
    @ApiOperation("获取个人用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pkid",value="用户ID"),
            @ApiImplicitParam(name="middle",value="个人用户0/1中间人用户"),
            @ApiImplicitParam(name="audit",value="个人用户(默认审核通过)/审核状态-1未审核/0未通过/1已通过"),
    })
    public ResultBean getUserList(){
        return ok("成功",userService.getUserList(ActionUtil.getParameterMap()));
    }
}
